package com.xgp.microChat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xgp.microChat.model.User;
import com.xgp.microChat.mapper.UserMapper;
import com.xgp.microChat.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 咸鱼
 * @since 2020-04-22
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    UserMapper userMapper;

    /**
     * 插入一个用户
     * @param user
     * @return
     */
    @Override
    public int insertUser(User user) {
        /**
         * 账号生成策略
         */
        String time = null;
        String account = null;
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        do {
            time = String.valueOf(System.currentTimeMillis());
            account = time.substring(time.length() - 10);
            wrapper.eq("account",account);
        }while (userMapper.selectCount(wrapper) != 0);

        user.setAccount(account);
        return userMapper.insert(user);
    }
}
